package com.ws.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ws.eduservice.client.OssClient;
import com.ws.eduservice.entity.EduCourse;
import com.ws.eduservice.entity.EduCourseDescription;
import com.ws.eduservice.entity.EduTeacher;
import com.ws.eduservice.entity.vo.CourseInfoForm;
import com.ws.eduservice.entity.vo.CoursePublishVo;
import com.ws.eduservice.frontvo.CourseWebVo;
import com.ws.eduservice.mapper.EduCourseMapper;
import com.ws.eduservice.service.EduChapterService;
import com.ws.eduservice.service.EduCourseDescriptionService;
import com.ws.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ws.eduservice.service.EduVideoService;
import com.ws.servicebase.exceptionhandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ws
 * @since 2024-12-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OssClient ossClient;

    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert == 0){
            throw new MyException(20001,"添加课程信息失败");
        }
        String cid = eduCourse.getId();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(cid);
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        courseDescriptionService.save(eduCourseDescription);
        return cid;
    }

    @Override
    public CourseInfoForm getCourseInfo(String couserId) {
       EduCourse eduCourse = this.getById(couserId);
        //System.out.println(eduCourse);
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse, courseInfoForm);

        EduCourseDescription eduCourseDescription = courseDescriptionService.getById(couserId);
        courseInfoForm.setDescription(eduCourseDescription.getDescription());

        return courseInfoForm;

    }

    @Override
    public String updateCourseInfo(CourseInfoForm courseInfoForm) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0){
            throw new MyException(20001,"更新课程信息失败");
        }
        String couserId = courseInfoForm.getId();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseInfoForm.getId());
        courseDescriptionService.updateById(eduCourseDescription);
        return couserId;
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String courseId) {
        CoursePublishVo coursePublishVoById = baseMapper.getCoursePublishVoById(courseId);
        return coursePublishVoById;
    }

    @Override
    public Boolean deleteCourseById(String courseId) {

        videoService.deleteByCourseId(courseId);

        chapterService.deleteByCourseId(courseId);

        courseDescriptionService.removeById(courseId);
        EduCourse eduCourse = baseMapper.selectById(courseId);
        System.out.println(eduCourse.getCover());
        ossClient.deleteFile(eduCourse.getCover());
        int i = baseMapper.deleteById(courseId);

        return i>0;
    }

    @Override
    public CourseWebVo getCourseFront(String id) {
        CourseWebVo courseWebVo=baseMapper.getCourseFront(id);
        return courseWebVo;
    }
}
